package com.android.image;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.mobclick.android.f;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;

public class ImageIOThread extends Handler {
	
    public static final int MSG_SAVE_BITMAP = 0;
    private static final String THREAD_NAME = "ImageIOThread";
    private String mImagePath;
    private static ImageIOThread mImageIOThread;
    private static HandlerThread mCacheThread;

    private ImageIOThread(Looper looper){
        super(looper);
        mImagePath = VGongyiApp.DEFAULT_SDCARD_PATH ;
    }
    
    public static ImageIOThread getInstance() throws SdCardNotAvailableException{
        if (VGongyiApp.DEFAULT_SDCARD_PATH == null) {
            throw new SdCardNotAvailableException("SD card is not available!");
        }
        
        if (mCacheThread == null) {
            mCacheThread = new HandlerThread(THREAD_NAME);
            mCacheThread.start();
        }
        if (mImageIOThread == null) {
            mImageIOThread = new ImageIOThread(mCacheThread.getLooper());
        }
        return mImageIOThread;
    }
	
    /**
     * stop and cancle the IO thread 
     */
    public void stopImageIO() {
        removeCallbacksAndMessages(null);
        try {
            if (mCacheThread != null) {
                mCacheThread.join();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        mCacheThread = null;
        mImageIOThread = null;
    }
	  
	
	 //save on the sdcard
    public void saveBitmap(CacheImage cacheImage) throws IOException {
        String fileName = cacheImage.fileName;
        if (fileName == null) {
            return;
        }
        InputStream in = cacheImage.stream;
        if (in == null) {
            return;
        }
        File file = new File(mImagePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        File imageFile = new File(mImagePath, fileName);
        if (!imageFile.exists()) {
        	
			FileOutputStream fOut = new FileOutputStream(imageFile);
			
			int count = 0;
			try {

				while (count < 3) {

					byte[] buffer = new byte[1024];

					while (in.read(buffer) != -1) {
						fOut.write(buffer);
					}

					count = 3;

				}

			} catch (Exception e) {
				count++;
			} finally {
				if (fOut != null) {
					fOut.flush();
					fOut.close();
				}

				if (in != null)
					in.close();
					cacheImage = null;
			}
        }
    }
    
    public static class CacheImage {
        
        public CacheImage (InputStream in , String name){
        	stream = in ;
        	fileName  = name;
        }

        Bitmap imageBitmap;
        String fileName;
        InputStream stream ;
    }
}
